<template>
	<view class="container">
		<tui-tabs :tabs="tabs" :isFixed="scrollTop>=0" :currentTab="currentTab" selectedColor="#E41F19"
			sliderBgColor="#E41F19" @change="change" itemWidth="20%"></tui-tabs>
		<view :class="{'tui-order-list':scrollTop>=0}">
			<view class="tui-order-item" v-for="(model,orderIndex) in list" :key="orderIndex">
				<tui-list-cell :hover="false" :lineLeft="false">
					<view class="tui-goods-title">
						<view>订单号：{{ model.aftersale_sn }}</view>
						<view class="tui-order-status" style="color: #888;">{{ model.aftersale_status_text }}</view>
					</view>
				</tui-list-cell>
				<block>
					<tui-list-cell padding="0">
						<view class="tui-goods-item" @click.stop="detail(model)">
							<image :src="info.app.cdnurl+ model.goods_image" class="tui-goods-img"></image>
							<view class="tui-goods-center">
								<view class="tui-goods-name">{{model.goods_title}}</view>
								<view class="tui-goods-attr">{{model.goods_sku_text}}</view>
							</view>
							<view class="tui-price-right">
								<view>￥{{model.goods_price}}</view>
								<view>x{{model.goods_num}}</view>
							</view>
						</view>
						<view class=""
							style="display: flex;justify-content: space-between;padding: 20rpx 30rpx 20rpx 30rpx;">
							<view class="" style="display: flex;font-size: 22rpx;">
								<view class="" style="font-size: 26rpx;">{{model.type_text}}</view>
								<view class="" style="color: #666666;margin-left: 20rpx;">
									{{model.aftersale_status_desc}}</view>
							</view>
							<view class="">
								<text class="tn-icon-right"></text>
							</view>

						</view>
					</tui-list-cell>
				</block>

				<tui-list-cell :hover="false" unlined>
					<view class="tui-goods-price">
						<view class="tui-price-large">总金额：{{model.pay_fee}}</view>
					</view>
				</tui-list-cell>
				<view class="tui-order-btn">
					<view class="tui-btn-ml" v-for="(item,index) in model.btns" :key="index">
						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							v-if="item=='cancel'" @click="cancel(model)">取消申请</tui-button>
						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							v-if="item=='delete'" @click="deletes(model)">删除</tui-button>


						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							v-if="item=='pay'" @click="payOrderDetail(model)">继续支付</tui-button>
						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							style="background: #f6f6f6;border: none;" v-if="item=='express'">查看物流</tui-button>

						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							v-if="item=='comment'" @click="addEvaluate(model)">评价晒单</tui-button>

						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							v-if="item=='confirm'" @click="confim(model)">确认收货</tui-button>

						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							v-if="item=='apply_refund'">申请退款</tui-button>



					</view>
				</view>
			</view>


		</view>

		<uni-popup ref="cancelDialog" type="dialog">
			<uni-popup-dialog cancelText="关闭" confirmText="确定" title="提示" content="确定要取消订单吗?" @confirm="dialogCancel"
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog cancelText="关闭" confirmText="确定" title="提示" content="确定要删除订单吗?" @confirm="dialogConfirm"
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getShopOrderAftersale,
		getShopOrderAftersaleDelete,
		getShopOrderAftersaleCancel
	} from "@/apis"
	export default {
		data() {
			return {
				scrollTop: 0,
				tabs: [{
					name: "全部",
					type: 'all'
				}, {
					name: "申请中",
					type: 'nooper'
				}, {
					name: "处理中",
					type: 'ing'
				}, {
					name: "已完成",
					type: 'completed'
				}, {
					name: "已拒绝",
					type: 'refuse'
				}, ],
				currentTab: 0,
				page: 1,
				type: 1,
				size: 5,
				list: [],
				lastPage: null,
				currentItem:{}
			}
		},
		async onShow() {
			this.page = 1
			this.type = this.tabs[this.currentTab].type
			let res = await this.getList()
			this.list = res.data.data
			this.lastPage = res.data.last_page
		},
		methods: {
			async change(e) {
				this.currentTab = e.index
				this.page = 1
				this.type = this.tabs[this.currentTab].type
				let res = await this.getList()
				this.list = res.data.data
				this.lastPage = res.data.last_page
			},
			async getList() {
				let res = await getShopOrderAftersale(this.type, this.page, this.size)
				return res
			},
			async deletes(item) {
				this.$refs.cancelDialog.open()
				this.currentItem = item

			},
			async cancel(item) {
				this.$refs.alertDialog.open()
				this.currentItem = item
			
			},
			async dialogConfirm() {
				let ress = await getShopOrderAftersaleCancel(this.currentItem.id)

				if (ress.code == 1) {
					uni.showToast({
						title: ress.msg,
						icon: "none"
					})

					let res = await this.getList()
					this.list = res.data.data
					this.lastPage = res.data.last_page
				}
			},
			async dialogCancel() {
				let ress = await getShopOrderAftersaleDelete(this.currentItem.id)

				if (ress.code == 1) {
					uni.showToast({
						title: ress.msg,
						icon: "none"
					})

					let res = await this.getList()
					this.list = res.data.data
					this.lastPage = res.data.last_page
				}
			},
			dialogClose() {
				this.$refs.alertDialog.close()
				this.$refs.cancelDialog.close()
			},
		},
		async onReachBottom() {
			//只是测试效果，逻辑以实际数据为准
			if (this.page >= this.lastPage) {
				return
			}
			this.page += 1
			let res = await this.getList()
			// this.list = res.data.data
			this.lastPage = res.data.last_page
			if (res.data.data.length >= 1) {
				this.list = [...this.list, ...res.data.data]
			}
			// this.loadding = true
			// this.pullUpOn = true
			// setTimeout(() => {
			// 	this.loadding = false
			// 	this.pullUpOn = false
			// }, 1000)
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		}
	}
</script>


<style lang="scss" scoped>
	.container {
		padding-bottom: env(safe-area-inset-bottom);
	}

	.tui-order-list {
		margin-top: 80rpx;
	}

	.tui-order-item {
		margin-top: 20rpx;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.tui-goods-title {
		width: 100%;
		font-size: 28rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.tui-order-status {
		color: #888;
		font-size: 26rpx;
	}

	.tui-goods-item {
		width: 100%;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
	}

	.tui-goods-img {
		width: 180rpx;
		height: 180rpx;
		display: block;
		flex-shrink: 0;
		border-radius: 20rpx;
	}

	.tui-goods-center {
		flex: 1;
		padding: 20rpx 8rpx;
		box-sizing: border-box;
	}

	.tui-goods-name {
		max-width: 310rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		font-size: 26rpx;
		line-height: 32rpx;
	}

	.tui-goods-attr {
		font-size: 22rpx;
		color: #888888;
		line-height: 32rpx;
		padding-top: 20rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.tui-price-right {
		text-align: right;
		font-size: 24rpx;
		color: #888888;
		line-height: 30rpx;
		padding-top: 20rpx;
	}

	.tui-color-red {
		color: #E41F19;
		padding-right: 30rpx;
	}

	.tui-goods-price {
		width: 100%;
		display: flex;
		align-items: flex-end;
		justify-content: flex-end;
		font-size: 24rpx;
	}

	.tui-size-24 {
		font-size: 24rpx;
		line-height: 24rpx;
	}

	.tui-price-large {
		font-size: 30rpx;
		line-height: 30rpx;
		font-weight: 500;
	}

	.tui-order-btn {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		background: #fff;
		padding: 10rpx 30rpx 20rpx;
		box-sizing: border-box;
	}

	.tui-btn-ml {
		margin-left: 20rpx;
	}

	.marginright {
		margin-right: 10rpx;
	}
</style>